//
//  FMDownLoadManager.m
//  FMDownLoad
//
//  Created by apple on 17/3/22.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "FMDownLoadManager.h"
#import "NSString+MD5.h"

@interface FMDownLoadManager ()<NSCopying, NSMutableCopying>

@property (nonatomic, strong) NSMutableDictionary *infoDic;

@end

@implementation FMDownLoadManager

static FMDownLoadManager *_shareInstance;

+ (instancetype)shareInstance{
    if (!_shareInstance) {
        _shareInstance = [[FMDownLoadManager alloc] init];
    }
    return _shareInstance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    if (!_shareInstance) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _shareInstance = [super allocWithZone:zone];
        });
    }
    return _shareInstance;
}

- (id)copyWithZone:(NSZone *)zone{
    return _shareInstance;
}

- (id)mutableCopyWithZone:(NSZone *)zone{
    return _shareInstance;
}

- (FMDownLoader *)downLoadWithURL: (NSURL *)url{
    NSString *md5 = [url.absoluteString md5Str];
    FMDownLoader *loader = self.infoDic[md5];
    if (loader) {
        [loader resume];
        return loader;
    }
    loader = [[FMDownLoader alloc] init];
    [self.infoDic setValue:loader forKey:md5];
    
    __weak typeof(self) weakSelf = self;
    [loader downLoadedWithUrl:url downLoadInfoTypeBlock:nil successBlock:^(NSString *cacheFilePath) {
        [weakSelf.infoDic removeObjectForKey:md5];
    } failBlock:^(NSString *errorMsg){
        [weakSelf.infoDic removeObjectForKey:md5];
    }];
    return loader;
}

- (void)downLoadedWithUrl:(NSURL *)url downLoadInfoTypeBlock:(DownLoadInfoTypeBlock)downLoadInfoTypeBlock successBlock:(DownLoadSuccessBlock)successBlock failBlock:(DownLoadFailTypeBlock)failBlock{
    //思路
    //1.先获取下载器容器中获取 看是否存在
    //2.有 直接下载 没有的话 直接添加到容器中 然后调用下载器中的方法
    //3.下载完成后 把下载器清除掉
    
    //1.先获取下载器容器中获取 看是否存在
    NSString *keyStr = [url.absoluteString md5Str];
    FMDownLoader *downLoader = self.infoDic[keyStr];
    if (downLoader == nil) {
        downLoader = [[FMDownLoader alloc] init];
        self.infoDic[keyStr] = downLoader;
    }
    
    //2.下载
    __weak typeof(self) weakSelf = self;
    [downLoader downLoadedWithUrl:url downLoadInfoTypeBlock:downLoadInfoTypeBlock successBlock:^(NSString *cacheFilePath) {
        [weakSelf.infoDic removeObjectForKey:keyStr];
        if (successBlock) {
            successBlock(cacheFilePath);
        }
    } failBlock:^(NSString * errorMsg){
        [weakSelf.infoDic removeObjectForKey:keyStr];
        if (failBlock) {
            failBlock(errorMsg);
        }
    }];

}

/**
 *  根据URL获取下载器
 *
 *  @param url url
 *
 *  @return 返回下载器
 */
- (FMDownLoader *)getCurrentDownLoaderWithURL:(NSURL *)url{
    NSString *md5 = [url.absoluteString md5Str];
    FMDownLoader *downLoader = self.infoDic[md5];
    return downLoader;
}

- (void)pauseWithURL:(NSURL *)url{
    NSString *keyStr = [url.absoluteString md5Str];
    FMDownLoader *downLoader = self.infoDic[keyStr];
    [downLoader pause];
}

- (void)pauseAll{
    [self.infoDic.allValues performSelector:@selector(pause) withObject:nil];
}

- (void)resumeAll{
//    [self.infoDic.allValues performSelector:@selector(resume) withObject:nil];
    [self.infoDic.allValues makeObjectsPerformSelector:@selector(resume)];

}

- (void)cancelWithURL:(NSURL *)url{
    NSString *keyStr = [url.absoluteString md5Str];
    FMDownLoader *downLoader = self.infoDic[keyStr];
    [downLoader pause];
}

- (void)cancelAndCleanWithURL:(NSURL *)url{
    NSString *keyStr = [url.absoluteString md5Str];
    FMDownLoader *downLoader = self.infoDic[keyStr];
    [downLoader cancelAndClean];
}


#pragma mark-------懒加载
- (NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionaryWithCapacity:1];
    }
    return _infoDic;
}

@end
